package com.ruoyi.web.controller.exampaper;

import java.util.List;

import com.ruoyi.common.viewmodel.ExamPaperVo;
import com.ruoyi.common.viewmodel.exam.ExamPaperEditRequestVM;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exampaper.domain.ExamPaper;
import com.ruoyi.exampaper.service.IExamPaperService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷列表Controller
 *
 * @author ruoyi
 * @date 2023-06-25
 */
@Controller
@RequestMapping("/exampaper/exampaperList")
public class ExamPaperController extends BaseController {
    private String prefix = "exampaper/exampaperList";

    @Autowired
    private IExamPaperService examPaperService;

    @RequiresPermissions("exampaper:exampaperList:view")
    @GetMapping()
    public String exampaperList() {
        return prefix + "/exampaperList";
    }

    /**
     * 查询试卷列表列表
     */
    @RequiresPermissions("exampaper:exampaperList:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamPaper examPaper) {
        startPage();
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        return getDataTable(list);
    }

    /**
     * 导出试卷列表列表
     */
    @RequiresPermissions("exampaper:exampaperList:export")
    @Log(title = "试卷列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamPaper examPaper) {
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        ExcelUtil<ExamPaper> util = new ExcelUtil<ExamPaper>(ExamPaper.class);
        return util.exportExcel(list, "试卷列表数据");
    }

    /**
     * 新增试卷列表
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存试卷列表
     */
    @RequiresPermissions("exampaper:exampaperList:add")
    @Log(title = "试卷列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamPaperEditRequestVM examPaperEditRequestVM) {
        return toAjax(examPaperService.insertExamPaper(examPaperEditRequestVM));
    }

    /**
     * 修改试卷列表
     */
    @RequiresPermissions("exampaper:exampaperList:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ExamPaperVo examPaper = examPaperService.selectExamPaperById(id);
        mmap.put("examPaper", examPaper);
        return prefix + "/edit";
    }

    /**
     * 在线考试，复用根据ID查询试卷的方法
     */
    @RequiresPermissions("exampaper:exampaperList:onlineexam")
    @GetMapping("/onlineexam/{id}")
    public String onlineexam(@PathVariable("id") Long id, ModelMap mmap) {
        ExamPaperVo examPaper = examPaperService.selectExamPaperById(id);
        mmap.put("examPaper", examPaper);
        return prefix + "/onlineexam";
    }

    /**
     * 修改保存试卷列表
     */
    @RequiresPermissions("exampaper:exampaperList:edit")
    @Log(title = "试卷列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamPaperEditRequestVM examPaperEditRequestVM) {
        return toAjax(examPaperService.updateExamPaper(examPaperEditRequestVM));
    }

    /**
     * 删除试卷列表
     */
    @RequiresPermissions("exampaper:exampaperList:remove")
    @Log(title = "试卷列表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(examPaperService.deleteExamPaperByIds(ids));
    }
}
